﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderLibrary;
namespace OrderWinForm
{
    public partial class Form2 : Form
    {
        Order On;
        OrderDetails ODn;
        public Order order1;
        public OrderDetails orderdetail1;
        public Form2()
        {
            InitializeComponent();
            On = new Order();
            ODn = new OrderDetails();
            OnoTextBox.DataBindings.Add("Text", On, "Ono");
            GoodNameTextBox.DataBindings.Add("Text", On, "Gname");
            CusNameTextBox.DataBindings.Add("Text", On, "Customer");
            OidTextBox.DataBindings.Add("Text", ODn, "OId");
            GNameTextBox.DataBindings.Add("Text", ODn, "Goodname");
            GoodNumTextBox.DataBindings.Add("Text", ODn, "GoodNum");
            UnitPriceTextBox.DataBindings.Add("Text", ODn, "UnitPrice");

        }
        public Form2(Order order,OrderDetails orderdetail)
        {
            InitializeComponent();
            order1 = order;
            orderdetail1 = orderdetail;
            OnoTextBox.DataBindings.Add("Text", order1, "Ono");
            GoodNameTextBox.DataBindings.Add("Text", order1, "Gname");
            CusNameTextBox.DataBindings.Add("Text", order1, "Customer");
            OidTextBox.DataBindings.Add("Text", orderdetail1, "OId");
            GNameTextBox.DataBindings.Add("Text", orderdetail1, "Goodname");
            GoodNumTextBox.DataBindings.Add("Text", orderdetail1, "GoodNum");
            UnitPriceTextBox.DataBindings.Add("Text", orderdetail1, "UnitPrice");

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)//退出
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)//创建订单·
        {
            bool equal = false;
            Form1 form1 = (Form1)this.Owner;
            foreach (Order i in form1.Orders.orderData)
            {
                if (i.Equals(On))
                {
                    i.Orderss.Add(ODn);
                    i.theWholePrice();
                    equal = true;
                }
            }
            if (!equal)
            {
                On.Orderss.Add(ODn);
                form1.Orders.orderData.Add(On);
                On.theWholePrice();

            }
            form1.OrderBindingSource.ResetBindings(false);
            this.Close();

        }

        private void button3_Click(object sender, EventArgs e)//修改订单
        {
            Form1 form1 = (Form1)this.Owner;
            form1.Orders.orderData.ForEach(i =>
            {
                if (i.Ono == order1.Ono)
                {
                    i.Gname = order1.Gname;
                    i.Customer = order1.Customer;
                    i.Orderss.ForEach(m =>
                    {
                        if (m.OId == orderdetail1.OId)
                        {
                            m.Goodname = orderdetail1.Goodname;
                            m.GoodNum = orderdetail1.GoodNum;
                            m.UnitPrice = orderdetail1.UnitPrice;
                        }
                    });
                    i.theWholePrice();
                }
            });
            form1.OrderBindingSource.ResetBindings(false);
            this.Close();
            
        }
    }
}
